﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Plugin;
using System.ComponentModel.Composition;
using COFE.Core;
using COFE.Core.Profile;
using COFE.Data.Utils;
using COFE.Core.Actions;
using NUnit.Framework;

namespace COFE.Data.Plugin
{
    [Export(typeof(IPluginUser))]
    public class COFEDBPluginUser : PluginUserBase
    {
        public COFEDBPluginUser()
            : base(PluginBase.Priority_COFEDB)
        {
        }

        protected IEnumerable<T> GetAllCOFEDBProperty<T>(IPluginManager manager,
           Func<ICOFEDBPlugin, IEnumerable<T>> propertyGetFunc)
        {
            return base.GetAllProperty<ICOFEDBPlugin, T>(manager, propertyGetFunc);
        }

        public override void Apply(IPluginManager m)
        {
            Assert.IsInstanceOf<DBWorkScheduler>(COFEActionShared.WorkScheduler);
   
            foreach (var exp in
                GetAllCOFEDBProperty(m, (p) => p.EntryFilterExpressions))
                DBFilterUtils.RegisterFilterExpression(exp);

            foreach (var exp in
              GetAllCOFEDBProperty(m, (p) => p.OptionFilterExpressions))
                DBFilterUtils.RegisterFilterExpression(exp);

            COFEShared.UserProfileManager.SetCurrentUserProfile(COFEShared.AdminUserProfile);

            var findExpireWork = new FindExpiredEntryWork(Guid.NewGuid(), 5, 0, 1);
            COFEActionShared.WorkScheduler.Enqueue(WorkPriority.Lower,
                TimeSpan.FromSeconds(10), findExpireWork);

            COFEShared.UserProfileManager.SetCurrentUserProfile(null);
        }

    }
}
